package com.kx.center.email;

import java.util.Date;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public abstract class SendMailBase {
	   /**
	    * 发送邮件主机
	    */
	   private String host = "localhost";
	  
	   /**
	    * 连接到邮件发送服务器端口
	    */
	   private int port =25;
	  
	   /**
	    * 邮件发送协议
	    */
	   private String protocol = "smtp";
	  
	   /**
	    * 账户
	    */
	   private String username = "aaa";
	  
	   /**
	    * 密码
	    */
	   private String password = "123";
	  
	   /**
	    * 发件人邮箱地址
	    */
	   private String addresser = "aaa@jianguo6.com";
	  
	   /**
	    * 收件人邮箱地址
	    */
	   private String recipient;
	  
	   /**
	    * 邮件主题
	    */
	   private String subject;
	  
	   /**
	    * 邮件模板位置
	    */
	   private String templateLocation;
	 
	   public String getHost() {
	      return host;
	   }
	 
	   public void setHost(String host) {
	      this.host = host;
	   }
	 
	   public int getPort() {
	      return port;
	   }
	 
	   public void setPort(int port) {
	      this.port = port;
	   }
	 
	   public String getProtocol() {
	      return protocol;
	   }
	 
	   public void setProtocol(String protocol) {
	      this.protocol = protocol;
	   }
	 
	   public String getUsername() {
	      return username;
	   }
	 
	   public void setUsername(String username) {
	      this.username = username;
	   }
	 
	   public String getPassword() {
	      return password;
	   }
	 
	   public void setPassword(String password) {
	      this.password = password;
	   }
	 
	   public String getAddresser() {
	      return addresser;
	   }
	 
	   public void setAddresser(String addresser) {
	      this.addresser = addresser;
	   }
	 
	   public String getRecipient() {
	      return recipient;
	   }
	 
	   public void setRecipient(String recipient) {
	      this.recipient = recipient;
	   }
	 
	   public String getSubject() {
	      return subject;
	   }
	 
	   public void setSubject(String subject) {
	      this.subject = subject;
	   }
	 
	   public String getTemplateLocation() {
	      return templateLocation;
	   }
	 
	   public void setTemplateLocation(String templateLocation) {
	      this.templateLocation = templateLocation;
	   }
	  
	   /**
	    * 发送邮件, 在这方法内部会创建一封邮件，然后根据Velocity模板和传入Map模型，合并出邮件内容，并发送出去
	    * @parammodel
	    * @throwsException
	    */
	   @SuppressWarnings("rawtypes")
	   public void sendMail(Map model) throws Exception{
	      JavaMailSenderImpl sender = new JavaMailSenderImpl();
	      sender.setHost(host);
	      sender.setUsername(username);
	      sender.setPassword(password);
	     
	      MimeMessage message = sender.createMimeMessage();
	      MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); //为了解决中文乱码问题
	     
	      //设置发件人
	      helper.setFrom(addresser);
	     
	      //设置收件人
	      String recipientInModel =(String) model.get("recipient");
	      if(recipientInModel != null&& !"".equals(recipientInModel.trim())){
	        helper.setTo(recipientInModel);
	      }
	      else{
	        helper.setTo(recipient);
	      }
	     
	      //设置邮件主题
	      helper.setSubject(subject);
	      //设置邮件发送时间
	      helper.setSentDate(new Date());
	  
	      //设置邮件正文
	      String text =createMailText(model);
	      helper.setText(text, true); //如果模板里是html代码, 那么这个一定要设置为true
	     
	      //发送邮件
	      sender.send(message);
	   }
	 
	   /**
	    * 生成邮件正文
	    * @parammodel
	    * @return
	    */
	   protected abstract String createMailText(Map model);
	}